package com.educal.Controller;

import com.educal.Bean.Admin;
import com.educal.Repository.AdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by miyang on 2017/12/17.
 */
@RestController
public class AdminController {
    @Autowired
    private AdminRepository adminRepository;

    /*
    * 增加学生*/
    @PostMapping(value = "/admins")
    public Admin addAdmin(@RequestParam("adminId") String adminId,
                          @RequestParam("adminName") String adminName,
                          @RequestParam("adminPassword") String adminPassword){
        Admin admin = new Admin();

        admin.setAdminid(adminId);
        admin.setAdminname(adminName);
        admin.setAdminpassword(adminPassword);

        return adminRepository.save(admin);
    }

    /*
    * 查询全部学生信息*/
    @GetMapping(value= "/admins")
    public List<Admin> findAll() {

        return adminRepository.findAll();
    }

    /*
    * 用id查询*/
    @GetMapping(value = "/admins/{adminId}")
    public Admin findByAdminId(@PathVariable("adminId") String adminId) {
        Admin admin = new Admin();
        List<Admin> list = adminRepository.findAll();
        for(int i=0;i<list.size();i++){
            String aId = list.get(i).getAdminid();
            if(aId.equals(adminId)){
                admin.setAdminid(adminId);
                admin.setAdminname(list.get(i).getAdminname());
                admin.setAdminpassword(list.get(i).getAdminpassword());
                break;
            }
        }
        return admin;
    }
    /*
    * 用name查询(报错！！！！！)*/
    /*@PostMapping(value = "/stu/find/name")
        public List<Student> findByName(@RequestParam("stuName") String stuName) {
        List<Student> stuList = null;
        int k = 0;
        List<Student> list = stuRepository.findAll();
        for(int i=0;i<list.size();i++){
            String sName = list.get(i).getStuname();
            if(sName.equals(stuName)){
                stuList.get(k).setStuid(list.get(i).getStuid());
                System.out.print(list.get(i).getStuid());
                stuList.get(k).setStuname(sName);
                stuList.get(k).setStucollege(list.get(i).getStucollege());
                stuList.get(k).setStuclass(list.get(i).getStuclass());
                k++;
            }
        }
        return stuList;
    }*/


    /*
    * 修改学生信息*/
    @PutMapping(value = "/admins")
    public Admin updateAdminId(@RequestParam("adminId") String adminId,
                               @RequestParam("adminName") String adminName,
                               @RequestParam("adminPassword") String adminPassword){
        Admin admin = new Admin();

        admin.setAdminid(adminId);
        admin.setAdminname(adminName);
        admin.setAdminpassword(adminPassword);

        return adminRepository.save(admin);
    }

    /*
    * 用id删除学生*/
    @DeleteMapping(value = "/admins/{adminId}")
    public void deleteAdmin(@PathVariable("adminId") String adminId){
        Admin admin = new Admin();
        List<Admin> list = adminRepository.findAll();
        for(int i=0;i<list.size();i++){
            String aId = list.get(i).getAdminid();
            if(aId.equals(adminId)){
                admin.setAdminid(adminId);
                admin.setAdminname(list.get(i).getAdminname());
                admin.setAdminpassword(list.get(i).getAdminpassword());
                adminRepository.delete(admin);
                break;
            }
        }
    }
}
